﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebCrawler.ProcessingFiles
{
    public partial class RedirectForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Type"].ToString() == "1")
            {
                if(Request.QueryString["Name"].ToString() == "Cat")
                {

                    ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                    var query = (from c in dc.Categories where c.process == 0 && c.exclude == 0 select c).FirstOrDefault();

                    if (query != null)
                    {
                        query.process = 1;
                        dc.SubmitChanges();
                    
                    }

                    GetNextPage("Cat");
                }
                if (Request.QueryString["Name"].ToString() == "Brand")
                {

                    ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                    var query = (from b in dc.Brands where b.process == 0 select b).FirstOrDefault();

                    if (query != null)
                    {
                        query.process = 1;
                        dc.SubmitChanges();

                    }

                    GetNextPage("Brand");
                }
                else if (Request.QueryString["Name"].ToString() == "Main")
                {

                    GetNextPage("Main");
                }

            }
            if (Request.QueryString["Type"].ToString() == "2")
            {
            
                GetNextPage(Request.QueryString["Name"].ToString().Trim());
               
            }
        }

        protected void GetNextPage(string Next)
        {
            if (Next == "Main" || Next == "Cat")
            {
                Response.Redirect("/ProcessingFiles/RunProcess.aspx");
            }
            else if (Next == "Brand")
            {
                Response.Redirect("/ProcessingFiles/ProcessBrands.aspx");
            }
            else if (Next == "Prod")
            {
                Response.Redirect("/ProcessingFiles/ProcessProducts.aspx");
            }
            else if (Next == "Predict")
            {
                Response.Redirect("/ProcessingFiles/ProcessOneProduct.aspx");
            }
        }
    }
}